/*
 *  Copyright (C) 2011 JiangHongTiao <jjurco.sk_gmail.com>
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package sk.lieskove301.jianghongtiao.liaad.prisoner.memory;

import java.util.Deque;
import java.util.Iterator;
import sk.lieskove301.jianghongtiao.liaad.prisoner.strategy.FightResultEnum;

/**
 * Date of create: May 15, 2011
 *
 * @author JiangHongTiao <jjurco.sk_gmail.com>
 * @version 2011.0515
 */
public class RememberBCMemory implements Forgetting{

    public FightResultEnum countBetrayMemories(Deque<FightResultEnum> memory) {
        if((memory == null) || memory.isEmpty()){
            throw new IllegalArgumentException("memory is null or empty");
        }
        boolean me = true;
        boolean him = true;
        for (Iterator it = memory.iterator(); it.hasNext();) {
            FightResultEnum item = (FightResultEnum) it.next();
            me &= !((item == FightResultEnum.BetrayCooperate) ||
                    (item == FightResultEnum.BothBetray));
            him &= !((item == FightResultEnum.CooperateBetray) ||
                    (item == FightResultEnum.BothBetray));
        }
        if(me && him) return FightResultEnum.BothCooperate;
        if(me && !him) return FightResultEnum.CooperateBetray;
        if(!me && him) return FightResultEnum.BetrayCooperate;
        if(!me && !him) return FightResultEnum.BothBetray;
        throw new IllegalArgumentException("Shit happened!");
    }

    public FightResultEnum countCooperateMemories(Deque<FightResultEnum> memory) {
        if((memory == null) || memory.isEmpty()){
            throw new IllegalArgumentException("memory is null or empty");
        }
        boolean me = false;
        boolean him = false;
        for (Iterator it = memory.iterator(); it.hasNext();) {
            FightResultEnum item = (FightResultEnum) it.next();
            me  |= ((item == FightResultEnum.CooperateBetray) ||
                    (item == FightResultEnum.BothCooperate));
            him |= ((item == FightResultEnum.BetrayCooperate) ||
                    (item == FightResultEnum.BothCooperate));
        }
        if(me && him) return FightResultEnum.BothCooperate;
        if(me && !him) return FightResultEnum.CooperateBetray;
        if(!me && him) return FightResultEnum.BetrayCooperate;
        if(!me && !him) return FightResultEnum.BothBetray;
        throw new IllegalArgumentException("Shit happened!");
    }

    public String getName() {
        return "Remember BC Memory";
    }

    public String getLetter() {
        return "B";
    }

}
